
local data1 = 0.6
local data2 = 0.5
local data3 = 0.5
local data4 = 0.2

local function last_tip(self)
    local level = self.level or 0
    if level==0 then
        return
    end
    if level>30 then
        return ('|cff00ffff该英雄提升：|r|n|cffff9900属性提升|r+%.1f%%|n|cffff9900技能系数|r+%.1f%%|n|cffff9900伤害增幅|r+%.1f%%|n|cffff9900最终伤害|r+%.1f%%'):format(data1*level,data2*level,data3*level,data4*level)
    else
        return ('|cff00ffff该英雄提升：|r|n|cffff9900属性提升|r+%.1f%%|n|cffff9900技能系数|r+%.1f%%|n|cffff9900伤害增幅|r+%.1f%%'):format(data1*level,data2*level,data3*level)
    end
end

local function exit_tip(self)
    local level = self.level or 0
    if level==50 then
        return
    end
    if level>30 then
        return ('|cff999999下一级提升：|r|n|cffff9900属性提升|r+%.1f%%|n|cffff9900技能系数|r+%.1f%%|n|cffff9900伤害增幅|r+%.1f%%|n|cffff9900最终伤害|r+%.1f%%'):format(data1,data2,data3,data4)
    else
        return ('|cff999999下一级提升：|r|n|cffff9900属性提升|r+%.1f%%|n|cffff9900技能系数|r+%.1f%%|n|cffff9900伤害增幅|r+%.1f%%'):format(data1,data2,data3)
    end
end

local sum_tips = {
    '基础力量+5',
    '基础敏捷+5',
    '基础智力+5',
    '力量加成+0.5%',
    '敏捷加成+0.5%',
    '智力加成+0.5%',
    '攻击伤害加成+0.5%',
    '技能伤害加成+0.5%',
    '物理伤害加成+0.5%',
    '魔法伤害加成+0.5%',
    '最终伤害加成+0.5%',
}

function get_sum_tips(level)
    local tips = {}
    for a=1,level do
        tips[#tips + 1] = sum_tips[a]
    end
    local tip1 = table.concat(tips,'|n')
    local tip2 = sum_tips[level + 1]
    if tip2 then
        tip1 = ('%s|n|n|cff999999下一级|n%s|r'):format(tip1, tip2)
    end
    tip1 = ('%s|n|n|cff00ffff每8级熟练度提升1级总等级'):format(tip1)
    return tip1
end

local list = {}
local symbols        = require 'symbol.unit'
local slk            = require 'jass.slk'
local keys          = ac.register_dzscore3('TJ',40,1)
for i,v in ipairs(load_excel('技能数据.xlsx','英雄参数')) do
    local abi1 = v['技能1']
    local abi2 = v['技能2']
    local name = v.name
    local data = ac.lni('unit',k) or {}
    local id = symbols[name]
    local da = slk.unit[id]
    if abi1 and abi2 then
        local index = #list + 1
        table.insert(list,{
            id = index,
            art = da.art,
            key = keys[i],
            name = name,
            level = 0,
            tip = function(self)
                local tips = {}
                tips[#tips + 1] = last_tip(self)
                tips[#tips + 1] = exit_tip(self)
                return table.concat(tips,'|n|n')
            end,
            up_tip = function(self)
                return ('|cff00ff00升级需要|r|n图鉴积分|cffff9900 %s|r|n|n按住|cff00ffffALT|r一升到底'):format((self.level or 0)*10 + 100)
            end,
        })
    end  
end

ac['图鉴熟练度'] = list


ac.game:event '玩家-存档读取前'(function(_,player)
    local point = player:get_data('图鉴积分') or 0
    local sum1 = 0
    local sum2 = 0
    for _,info in ipairs(list) do
        local name = info.name
        local level = player:dz_get(info.key) or 0
        local key = ('%s熟练度'):format(name)
        local jf = 0
        for a=1,level do
            jf = jf + (a-1)*10 + 100
        end
        if point >= sum2 + jf then
            sum2 = sum2 + jf
        else
            level = 0
        end
        sum1 = sum1 + level
        player:set_data(key, level)
        if player:is_self() then
            info.level = level
        end
    end
    player:set_data('图鉴积分-剩余', point - sum2)
    player:set_data('图鉴总熟练度', sum1)
end)

ac.sync:event '同步-图鉴升级'(function(_,player,id,count)
    local info = list[id]
    if not info then
        return
    end
    local key = info.key
    local level = player:dz_get(key) or 0
    local point = player:get_data('图鉴积分-剩余') or 0
    local count = count or 1
    for a=1,count do
        if level >= 50 then
            return
        end
        local exp = level*10 + 100
        level = level + 1
        if point < exp then
            return '图鉴积分不足'
        end
        point = point - exp
        player:set_data('图鉴积分-剩余', point)
        player:set_data('图鉴总熟练度', player:get_data('图鉴总熟练度',0) + 1)
        player:dz_set(key, level)
        if player:is_self() then
            info.level = level
        end
    end


end)


ac.sync:event '同步-图鉴重置'(function(_,player)
    local point = player:get_data('图鉴积分') or 0
    player:set_data('图鉴积分-剩余', point)
    player:set_data('图鉴总熟练度', 0)
    if player:is_self() then
        for _,info in ipairs(list) do
            info.level = 0
        end
    end
end)
